package com.threeti.mecool.core.infrastructure.persistence.plan;

import com.threeti.mecool.core.domain.model.plan.pojo.PlanProject;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class PlanProjectSpecs {
    public static Specification<PlanProject> userLoginNameIs(final String userLoginName) {
        return new Specification<PlanProject>() {
            @Override
            public Predicate toPredicate(Root<PlanProject> planProjectRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.equal(planProjectRoot.get("plan").get("userLoginName"), userLoginName);
            }
        };
    }

    public static Specification<PlanProject> planNameLike(final String planName) {
        return new Specification<PlanProject>() {
            @Override
            public Predicate toPredicate(Root<PlanProject> planProjectRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.like(planProjectRoot.get("plan").<String>get("name"), "%" + planName + "%");
            }
        };
    }

    public static Specification<PlanProject> planTypeIs(final String type) {
        return new Specification<PlanProject>() {
            @Override
            public Predicate toPredicate(Root<PlanProject> planProjectRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.equal(planProjectRoot.get("plan").<String>get("type"), type);
            }
        };
    }

    public static Specification<PlanProject> assignedByIs(final String assignedBy) {
        return new Specification<PlanProject>() {
            @Override
            public Predicate toPredicate(Root<PlanProject> planProjectRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.equal(planProjectRoot.get("plan").get("assignedBy"), assignedBy);
            }
        };
    }
}
